<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.w3.org/1999/xhtml">
<head th:include="css :: css">
</head>
<link href="/js/jquery.icheck/skins/square/blue.css" rel="stylesheet">
<link href="/css/bootstrap-toggle.min.css" rel="stylesheet">
<link href="/js/jquery.icheck/skins/square/blue.css" rel="stylesheet">
<link href="/css/chosen.css" rel="stylesheet">
<body style="opacity: 1">
<div style="padding-top: 0">
    <div class="container-fluid">
        <div class="cl-mcont">
            <div class="row">
                <div class="col-md-12" style="padding:0">
                    <div class="block-flat">
                        <form class="form-horizontal group-border-dashed" id="buyCode">
                            <input type="hidden" id="productTypes" name="productTypes">
                            <input type="hidden" id="statusArray" name="statusArray">
                            <div class="form-group">
                                <div class="input-group">
                                    <input type="text" class="form-control" name="startTime"
                                           id="startTime" style="display: inline-block; width: 145px;">
                                    <input type="text" class="form-control" name="endTime"
                                           id="endTime" style="display: inline-block; width: 145px;">
                                </div>
                                <div class="input-group">
                                    <input type="text" class="form-control" id="batchId" name="batchId"
                                           placeholder="批次号"/>
                                </div>
                                <div class="input-group">
                                    <input type="text" class="form-control" id="orderId" name="orderId"
                                           placeholder="订单号"/>
                                </div>
                                <div class="input-group">
                                    <input type="text" class="form-control" id="productName" name="productName"
                                           placeholder="产品名称"/>
                                </div>
                                <div class="input-group">
                                    <input type="text" class="form-control" id="cardOperatorName"
                                           name="cardOperatorName"
                                           placeholder="运营商"/>
                                </div>
                                <div class="input-group">
                                    <input type="text" class="form-control" id="account" name="account"
                                           placeholder="用户手机号"/>
                                </div>
                                <div class="input-group">
                                    <input type="text" class="form-control" id="realName" name="realName"
                                           placeholder="真实姓名"/>
                                </div>
                                <div class="input-group">
                                    <input type="text" class="form-control" id="aliUserId"
                                           name="aliUserId"
                                           placeholder="支付宝用户ID"/>
                                </div>
                                <div class="input-group">
                                    <select class="form-control " id="transferStatus" name="transferStatus">
                                        <option value="" selected>转账状态</option>
                                        <option value="UN_TRANSFER">未转账</option>
                                        <option value="SUCCESS">转账成功</option>
                                        <option value="FAILED">转账失败</option>
                                    </select>
                                </div>
                                <div class="input-group">
                                    <select class="form-control " id="status" name="status">
                                        <option value="" selected>全部状态</option>
                                        <option value="0">在售</option>
                                        <option value="1">已售</option>
                                        <option value="2">停售</option>
                                        <option value="3">可售</option>
                                        <option value="4">未预判面值</option>
                                        <option value="5">预判面值不符</option>
                                        <option value="7">等待慢速试充</option>
                                        <option value="8">慢速试充</option>
                                        <option value="9">待结算</option>
                                        <option value="10">纠纷中</option>
                                        <option value="50">补结算</option>
                                    </select>
                                </div>
                                <div class="input-group">
                                    <select class="form-control " id="authenticationStatus" name="authenticationStatus">
                                        <option value="" selected>认证状态</option>
                                        <option value="0">未认证</option>
                                        <option value="1">认证成功</option>
                                        <option value="2">认证失败</option>
                                        <option value="3">认证中</option>
                                    </select>
                                </div>
                                <div class="input-group">
                                    <button class="btn btn-primary" type="button" id="search"><i
                                            class="fa fa-search"></i>查询
                                    </button>
                                </div>
                                <!-- <div class="input-group">&lt;!&ndash;shiro:hasAnyRoles="caiwu"&ndash;&gt;
                                     <button class="btn btn-success" type="button" id="transferAgain"
                                             onclick="updateAliProgramStatus(2)"><i
                                             class="fa fa-check-circle"></i>再次转账
                                     </button>
                                 </div>-->
                                <div class="input-group" shiro:hasAnyRoles="caiwu">
                                    <button class="btn btn-success" type="button" id="unauthorizedTransferAgain"
                                            onclick="unauthorizedTransferAccount()"><i
                                            class="fa fa-check-circle"></i>未认证转账
                                    </button>
                                </div>
                                <div class="input-group"><!--shiro:hasAnyRoles="caiwu"-->
                                    <span id="stopPro">
                                            <input type="checkbox" data-onstyle="danger" data-offstyle="warning"
                                                   name="include" id="include"
                                                   data-on="转账开启" data-off="转账关闭" checked data-toggle="toggle"
                                                   data-size="mini"/>
                                    </span>
                                </div>
                                <div class="input-group" id="export_Role">
                                    <button class="btn btn-default" type="button" id="export"><i
                                            class="fa fa-download"></i> 导出
                                    </button>
                                </div>
                            </div>
                        </form>
                    </div>
                    <div class="block-flat">
                        <div class="content">
                            <div class="table-responsive">
                                <table class="table hover" id="dataTable">
                                </table>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

        </div>
    </div>

</div>
<div th:include="js :: js"></div>
<script type="text/javascript" th:attr="src=@{/js/jquery.form.js}"></script>
<script type="text/javascript" th:attr="src=@{/js/jquery.parser.js}"></script>
<script type="text/javascript" src="js/jquery.icheck/icheck.min.js"></script>
<script type="text/javascript" src="/js/chosen.jquery.js?deVersion=20181411895"></script>
<script>
    $(function () {
        buttonShiro("export_Role");
        auditGet();
        var myDate = new Date();
        var month = myDate.getMonth() + 1;
        var day = myDate.getDate();
        if (month < 10) {
            month = "0" + month;
        }
        if (day < 10) {
            day = "0" + day;
        }
        var _start = myDate.getFullYear() + '-' + month + '-' + day + " 00:00:00";
        var _end = myDate.getFullYear() + '-' + month + '-' + day + " 23:59:59";
        $("#startTime").val(_start);
        $("#endTime").val(_end);
        laydate.render({
            elem: '#startTime',
            type: 'datetime'

        });
        laydate.render({
            elem: '#endTime'
            , type: 'datetime'
        });
        $('#dataTable').bootstrapTable({
            url: '/alipayProgram/queryPage',
            height: $(window).height() - 200,
            pageSize: 100,
            pagination: true,
            pageList: [30, 50, 100],
            clickToSelect: true,
            queryParams: queryParams,
            uniqueId: 'id',
            queryParamsType: '',
            sidePagination: 'server',
            showFooter: true,
            detailView: true,
            detailFormatter: function (index, row) {
                return getLog(row.id);
            },
            responseHandler: function (res) {
                return {
                    "total": res.total,//总页数
                    "rows": res.resultList   //数据
                };
            },
            columns: [
                {
                    checkbox: true,
                    align: 'center'
                },
                {
                    field: 'batchId',
                    title: '批次号/订单号',
                    width: 90, align: 'center',
                    formatter: function (v, r) {
                        return '<span>' + v + '</span>' + '</br><span>' + r.orderId + '</span>'
                    }
                },
                {
                    field: 'aliOrderId',
                    title: '支付宝流水号',
                    width: 90, align: 'center'
                }, {
                    field: 'cardOperatorName',
                    title: '运营商',
                    width: 90, align: 'center'
                }, {
                    field: 'productName',
                    title: '产品名称',
                    width: 90, align: 'center'
                }, {
                    field: 'transferStatus',
                    title: '转账状态',
                    width: 50,
                    align: 'center',
                    formatter: function (v, row, index) {
                        return transferStatus(v);
                    }
                },
                {
                    field: 'authenticationStatus',
                    title: '认证状态',
                    width: 90, align: 'center',
                    formatter: function (v, row, index) {
                        return authenticationStatus(v);
                    }
                },
                {
                    field: 'transferPrice',
                    title: '转账金额',
                    width: 50,
                    align: 'center'
                },
                {
                    field: 'realName',
                    title: '姓名/手机号',
                    width: 50,
                    align: 'center',
                    formatter: function (v, r) {
                        return '<span>' + (v == 'undefined' || v == null ? "" : v) + '</span>' + '</br><span>' + r.account + '<span>';
                    }
                },
                {
                    field: 'aliUserId',
                    title: '支付宝用户ID',
                    width: 50,
                    align: 'center'
                },
                {
                    field: 'status',
                    title: '订单状态',
                    width: 50,
                    align: 'center',
                    formatter: function (v, row, index) {
                        return convertStatus(v);
                    }
                },
                {
                    align: 'center', field: 'createTime', title: '时间', formatter: function (v, o) {
                        return "创建:" + (o.createTime != null ? o.createTime.split('.')[0] : "") + "<br/>" +
                            "修改:" + (o.modifiedTime != null ? o.modifiedTime.split('.')[0] : "");
                    }
                },
                {
                    align: 'center', field: 'sellingTime', title: '时间', formatter: function (v, o) {
                        return "销卡:" + (o.sellingTime != null ? o.sellingTime.split('.')[0] : "") + "<br/>" +
                            "" + (o.transferTime != null ? '转账:' + o.transferTime.split('.')[0] : "");
                    }
                },
                {
                    field: 'remark',
                    title: '备注',
                    width: 100,
                    align: 'center'
                }
            ]
        });
        $("#search").click(function () {
            var timeAdd = new Date($("#startTime").val());
            var timeEnd = new Date($("#endTime").val());
            if (timeAdd > timeEnd) {
                layer.msg("开始时间不能大于结束时间");
                return false;
            }
            var require = (timeEnd - timeAdd) / (1 * 24 * 60 * 60 * 1000);
            if (require > 7) {
                layer.msg("时间间隔不能超过7天");
                return;
            }
            $("#dataTable").bootstrapTable("refresh");
        });

        //下载
        $("#export").click(function () {
            var timeAdd = new Date($("#startTime").val());
            var timeEnd = new Date($("#endTime").val());
            var require = (timeEnd - timeAdd) / (1 * 24 * 60 * 60 * 1000);
            if (require > 6) {
                layer.msg("只能下载七天的数据");
                return;
            }
            window.location.href = "/alipayProgram/zfbOrderDetailDownLoad?" + $("#buyCode").serialize()
        })
        //转账开启关闭
        $('#stopPro').click(function (e) {
            var status;
            e.stopPropagation();
            var remind = "";
            var isOn = $('#include').prop('checked');
            if (isOn) {
                remind = "关闭";
                status = 0
            } else {
                remind = "开启"
                status = 1
            }
            $.confirm({
                title: '确认!',
                content: '确定要' + remind + '支付宝转账吗？',
                confirm: function () {
                    $.ajax({
                        type: "POST",
                        url: "/alipayProgram/changeTransferFlag",
                        data: {
                            status: status
                        },
                        dataType: "json",
                        success: function (data) {
                            if (data.success) {
                                layer.msg(data.message);
                                auditGet();
                            } else {
                                layer.msg(data.message);
                                auditGet();
                            }
                        }
                    })
                }
            })
        })

        function auditGet() {
            $.ajax({
                type: "POST",
                url: "/alipayProgram/getTransferFlag",
                success: function (data) {
                    if (data) {
                        $('#include').bootstrapToggle('on');
                    } else {
                        $('#include').bootstrapToggle('off');
                    }
                },
            });
        }

        function queryParams(params) {
            return {
                page: params.pageNumber,
                size: params.pageSize,
                startTime: $("#startTime").val(),
                endTime: $("#endTime").val(),
                batchId: $.trim($("#batchId").val()),
                orderId: $.trim($("#orderId").val()),
                transferStatus: $.trim($("#transferStatus").val()),
                status: $.trim($("#status").val()),
                account: $.trim($("#account").val()),
                realName: $.trim($("#realName").val()),
                cardOperatorName: $.trim($("#cardOperatorName").val()),
                productName: $.trim($("#productName").val()),
                authenticationStatus: $("#authenticationStatus").val(),
                aliUserId: $("#aliUserId").val()
            };
        }
    })

    //操作日志
    function getLog(id) {
        $.ajax({
            type: "post",
            url: "/finance/queryWithdrawrLog?withdrawId=" + id + "&type=ALI_TRANSFER",
            async: false,
            success: function (data) {
                o = data;
            }
        });
        var str = '';
        for (var i = 0; i < o.length; i++) {
            if (o[i].withdrawId === undefined || o[i].withdrawId === null || o[i].withdrawId === '') {
                str += ' ';
            } else {
                str += '<div style="float:left">' + o[i].admin + format(o[i].operateTime) + '  操作状态:'
                    + {
                        0: "未转账",
                        1: "转账成功",
                        2: "转账失败"
                    }[o[i].oldStatus] + '->' + {
                        0: "未转账",
                        1: "转账成功",
                        2: "转账失败"
                    }[o[i].status] + '</div><br/>';
            }
        }
        return str;
    }

    //未认证转账
    function unauthorizedTransferAccount() {
        var row = $('#dataTable').bootstrapTable("getSelections");
        if (row === null || row.length === 0) {
            layer.msg("请选择要操作的订单");
            return;
        }
        if (row.length > 1) {
            layer.msg("仅允许单笔操作，请确认！");
            return;
        }
        if (row[0].status != 1 || row[0].authenticationStatus != 0) {
            layer.msg("仅允许'已售'，且'未认证'状态操作转账！");
            return;
        }

        $.confirm({
            title: '确认!',
            content: '确定要操作转账吗？',
            confirm: function () {
                $.ajax({
                    type: "POST",
                    url: "/aliProgramTransfer/unauthorizedTransferAccount",
                    data: {
                        id: row[0].id,
                        transferStatus: row[0].transferStatus,
                        status: row[0].status,
                        authenticationStatus: row[0].authenticationStatus,
                        orderId: row[0].orderId,
                        transferPrice:row[0].transferPrice
                    },
                    success: function (data) {
                        if (data.success) {
                            $("#dataTable").bootstrapTable("refresh");
                            layer.msg(data.message);
                        } else {
                            layer.msg(data.message)
                        }
                    },
                });
            },
            cancel: function () {

            }
        });
    }

    //转账失败操作
    function updateAliProgramStatus(v) {
        var rows = $('#dataTable').bootstrapTable("getSelections");
        if (rows === null || rows.length === 0) {
            layer.msg("请选择要操作的订单");
            return;
        }
        if (rows.length > 1) {
            layer.msg("仅允许单笔操作，请确认！");
            return;
        }
        console.log(rows[0]);
        transferAgain(rows[0]);
    }

    //再次转账
    function transferAgain(row) {
        if (row.transferStatus != 'FAILED' || row.status != 1) {
            layer.msg("当前状态不允许修改！");
            return;
        }
        $.confirm({
            title: '确认!',
            content: '确定要再次转账吗？',
            confirm: function () {
                $.ajax({
                    type: "POST",
                    url: "/alipayProgram/transferAgain",
                    data: {
                        id: row.id,
                        transferStatus: row.transferStatus,
                        status: row.status
                    },
                    success: function (data) {
                        if (data.success) {
                            $("#dataTable").bootstrapTable("refresh");
                            layer.msg(data.message);
                        } else {
                            layer.msg(data.message)
                        }
                    },
                });
            },
            cancel: function () {

            }
        });
    }
</script>
</body>
</html>
